//
//  LrcTableView.m
//  kyMusicPlayer
//
//  Created by 翁科宇 on 2018/5/17.
//  Copyright © 2018年 翁科宇. All rights reserved.
//

#import "LrcTableView.h"

@interface LrcTableView ()

@end

@implementation LrcTableView
extern SongInfo *songInfo;
- (void)setLrc_Index:(int)lrc_index{
    if (_lrc_Index == lrc_index || lrc_index > songInfo.mLrcDictinary.count - 1) {
        return;
    }
    //滚动到制定的位置
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lrc_index inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    UITableViewCell *currentCell = [self.tableView cellForRowAtIndexPath:indexPath];
    _currentCell = (LrcTableViewCell *)currentCell;
    _currentCell.textLabel.textColor = [UIColor redColor];
    
    NSIndexPath *oldIndexPath = [NSIndexPath indexPathForRow:_lrc_Index inSection:0];
    UITableViewCell *oldCell = [self.tableView cellForRowAtIndexPath:oldIndexPath];
    _lrcOldCell = (LrcTableViewCell *)oldCell;
    _lrcOldCell.textLabel.textColor = [UIColor whiteColor];
    
    [_currentCell addAnimation:scaleAlways];
    // 更新歌词index
    _lrc_Index = lrc_index;
}


- (instancetype)initWithStyle:(UITableViewStyle)style{
    self = [super initWithStyle:style];
    if (self) {
        songInfo.mLrcDictinary = [[NSMutableDictionary alloc] init];
        songInfo.mTimeArray = [[NSMutableArray alloc] init];
//        if(_lockScreenTableView == nil){
//            _lockScreenTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 800-44 * 7 + 20, 480, 44 * 3) style:style];
//        }
    }
    return self;
}




- (void)KY_SetUpLrcTableViewFrame:(CGRect)frame{
    self.view.frame = frame;
}

- (void)AnalysisLRC:(NSString *)lrcPath{
    if (songInfo.isLrcExistFlg) {
        NSArray *lrcArray = [lrcPath componentsSeparatedByString:@"\n"];
        [songInfo.mLrcDictinary removeAllObjects];
        [songInfo.mTimeArray removeAllObjects];
        
        for (NSString *line in lrcArray) {
            /*首先处理歌词中无用的东西,[ti:][ar:][al:]这类的直接跳过*/
            if ([line containsString:@"[0"] || [line containsString:@"[1"] || [line containsString:@"[2"] || [line containsString:@"[3"]) {
                NSArray *lineArr = [line componentsSeparatedByString:@"]"];
                NSString *str1 = [line substringWithRange:NSMakeRange(3, 1)];
                NSString *str2 = [line substringWithRange:NSMakeRange(6, 1)];
                if ([str1 isEqualToString:@":"] && [str2 isEqualToString:@"."]) {
                    NSString *lrcStr = lineArr[1];
                    NSString *timeStr = [lineArr[0] substringWithRange:NSMakeRange(1, 5)];
                    [songInfo.mLrcDictinary setObject:lrcStr forKey:timeStr];
                    [songInfo.mTimeArray addObject:timeStr];
                }
            }else{
                continue;
            }
        }
        _mIsLRCPrepared = YES;
        [self.tableView reloadData];
        
    }
    else{
        [songInfo.mLrcDictinary removeAllObjects];
        [songInfo.mTimeArray removeAllObjects];
        [songInfo.mLrcDictinary setObject:@"没有找到该歌词" forKey:@"0:0"];
        [songInfo.mTimeArray addObject:@"0:0"];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
        [self.tableView reloadData];
    }
}

- (UIImage *)getBlurredImage:(UIImage *)image{
    CIContext *context = [CIContext contextWithOptions:nil];
    
    CIImage *ciImage = [CIImage imageWithCGImage:image.CGImage];
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:ciImage forKey:kCIInputImageKey];
    [filter setValue:@5.0f forKey:@"inputRadius"];
    
    CIImage *result = [filter valueForKey:kCIOutputImageKey];
    CGImageRef ref = [context createCGImage:result fromRect:[result extent]];
    return [UIImage imageWithCGImage:ref];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return songInfo.mLrcDictinary.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LrcTableViewCell *cell = [LrcTableViewCell KY_CellForRowWithTableView:tableView];
    cell.textLabel.text = songInfo.mLrcDictinary[songInfo.mTimeArray[indexPath.row]];
    cell.textLabel.font = [UIFont systemFontOfSize:16.0];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;//设置文字过长时的显示格式
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_currentRow == indexPath.row) {
        cell.textLabel.textColor = [UIColor redColor];
        
    }else{
        cell.textLabel.textColor = [UIColor whiteColor];
    }
    return cell;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    _isDragging = YES;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _isDragging = NO;
}






@end
